<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <?php $_itemsGridLabel = $block->getForcedShipmentCreate() ? 'Items to Invoice and Ship' : 'Items to Invoice'; ?>
        <span class="title"><?php /* @escapeNotVerified */ echo __('%1', $_itemsGridLabel) ?></span>
    </div>
    <div class="admin__page-section-content grid">
        <div class="admin__table-wrapper">
            <table class="data-table admin__table-primary order-invoice-tables">
                <thead>
                    <tr class="headings">
                        <th class="col-product"><span><?php /* @escapeNotVerified */ echo __('Product') ?></span></th>
                        <th class="col-price"><span><?php /* @escapeNotVerified */ echo __('Price') ?></span></th>
                        <th class="col-ordered-qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></th>
                        <th class="col-qty-invoice"><span><?php /* @escapeNotVerified */ echo __('Qty to Invoice') ?></span></th>
                        <th class="col-subtotal"><span><?php /* @escapeNotVerified */ echo __('Subtotal') ?></span></th>
                        <th class="col-tax"><span><?php /* @escapeNotVerified */ echo __('Tax Amount') ?></span></th>
                        <th class="col-discount"><span><?php /* @escapeNotVerified */ echo __('Discount Amount') ?></span></th>
                        <th class="col-total last"><span><?php /* @escapeNotVerified */ echo __('Row Total') ?></span></th>
                    </tr>
                </thead>
                <?php if ($block->canEditQty()): ?>
                <tfoot>
                    <tr>
                        <td colspan="2">&nbsp;</td>
                        <td colspan="3"><?php echo $block->getUpdateButtonHtml() ?></td>
                        <td colspan="3">&nbsp;</td>
                    </tr>
                </tfoot>
                <?php endif; ?>
                <?php $_items = $block->getInvoice()->getAllItems() ?>
                <?php $_i = 0; foreach ($_items as $_item): ?>
                <?php if ($_item->getOrderItem()->getParentItem()) {
                    continue;
                } else {
                    $_i++;
                } ?>
                    <tbody class="<?php /* @escapeNotVerified */ echo $_i%2 ? 'even' : 'odd' ?>">
                        <?php echo $block->getItemHtml($_item) ?>
                        <?php echo $block->getItemExtraInfoHtml($_item->getOrderItem()) ?>
                    </tbody>
                <?php endforeach; ?>
            </table>
        </div>
    </div>
</section>

<?php $orderTotalBar = $block->getChildHtml('order_totalbar'); ?>

<?php if (!empty($orderTotalBar)): ?>
<section class="admin__page-section">
    <?php /* @escapeNotVerified */ echo $orderTotalBar; ?>
</section>
<?php endif; ?>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?php /* @escapeNotVerified */ echo __('Order Total') ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-comments-history">
            <div class="admin__page-section-item-title">
                <span class="title"><?php /* @escapeNotVerified */ echo __('Invoice History') ?></span>
            </div>
            <div id="history_form" class="admin__page-section-item-content order-history-form">
                <div class="admin__field">
                    <label for="invoice_comment_text" class="admin__field-label">
                        <span><?php /* @escapeNotVerified */ echo __('Invoice Comments') ?></span>
                    </label>
                    <div class="admin__field-control">
                        <textarea id="invoice_comment_text" name="invoice[comment_text]" class="admin__control-textarea"
                                  rows="3" cols="5"><?php /* @escapeNotVerified */ echo $block->getInvoice()->getCommentText(); ?></textarea>
                    </div>
                </div>
            </div>
        </div>

        <div id="invoice_totals" class="admin__page-section-item order-totals">
            <div class="admin__page-section-item-title">
                <span class="title"><?php /* @escapeNotVerified */ echo __('Invoice Totals') ?></span>
            </div>
            <div class="admin__page-section-item-content order-totals-actions">
                <?php echo $block->getChildHtml('invoice_totals') ?>
                <?php if ($block->isCaptureAllowed()): ?>
                <?php if ($block->canCapture()):?>
                    <div class="admin__field">
                        <?php
                        /*
                        <label for="invoice_do_capture" class="normal"><?php echo __('Capture Amount') ?></label>
                        <input type="checkbox" name="invoice[do_capture]" id="invoice_do_capture" value="1" checked/>
                        */
                        ?>
                      <label for="invoice_do_capture" class="admin__field-label"><?php /* @escapeNotVerified */ echo __('Amount') ?></label>
                      <select class="admin__control-select" name="invoice[capture_case]">
                          <option value="online"><?php /* @escapeNotVerified */ echo __('Capture Online') ?></option>
                          <option value="offline"><?php /* @escapeNotVerified */ echo __('Capture Offline') ?></option>
                          <option value="not_capture"><?php /* @escapeNotVerified */ echo __('Not Capture') ?></option>
                      </select>
                    </div>
                <?php elseif ($block->isGatewayUsed()):?>
                    <input type="hidden" name="invoice[capture_case]" value="offline"/>
                    <div><?php /* @escapeNotVerified */ echo __('The invoice will be created offline without the payment gateway.') ?></div>
                <?php endif?>
                <?php endif; ?>
                <div class="admin__field admin__field-option field-append">
                    <input id="notify_customer" name="invoice[comment_customer_notify]" value="1" type="checkbox"
                           class="admin__control-checkbox" />
                    <label class="admin__field-label" for="notify_customer"><?php /* @escapeNotVerified */ echo __('Append Comments') ?></label>
                </div>
                <?php if ($block->canSendInvoiceEmail()): ?>
                <div class="admin__field admin__field-option field-email">
                    <input id="send_email" name="invoice[send_email]" value="1" type="checkbox"
                           class="admin__control-checkbox" />
                    <label class="admin__field-label" for="send_email"><?php /* @escapeNotVerified */ echo __('Email Copy of Invoice') ?></label>
                </div>
                <?php endif; ?>
                <?php echo $block->getChildHtml('submit_before') ?>
                <div class="actions">
                    <?php echo $block->getChildHtml('submit_button') ?>
                    <?php echo $block->getChildHtml('submit_after') ?>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
require(['jquery', 'prototype'], function(jQuery){

//<![CDATA[
var submitButtons = $$('.submit-button');
var updateButtons = $$('.update-button');
var enableSubmitButtons = <?php echo (int) !$block->getDisableSubmitButton() ?>;
var fields = $$('.qty-input');

updateButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});

for(var i=0;i<fields.length;i++){
    jQuery(fields[i]).on('keyup', checkButtonsRelation);
    fields[i].baseValue = fields[i].value;
}

function checkButtonsRelation() {
    var hasChanges = false;
    fields.each(function (elem) {
        if (elem.baseValue != elem.value) {
            hasChanges = true;
        }
    }.bind(this));
    if (hasChanges) {
        submitButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});
        updateButtons.each(function (elem) {elem.disabled=false;elem.removeClassName('disabled');});
    }
    else {
        if (enableSubmitButtons) {
            submitButtons.each(function (elem) {elem.disabled=false;elem.removeClassName('disabled');});
        }
        updateButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});
    }
}

var sendEmailCheckbox = $('send_email');
if (sendEmailCheckbox) {
    var notifyCustomerCheckbox = $('notify_customer');
    var invoiceCommentText = $('invoice_comment_text');
    Event.observe(sendEmailCheckbox, 'change', bindSendEmail);
    bindSendEmail();
}
function bindSendEmail()
{
    if (sendEmailCheckbox.checked == true) {
        notifyCustomerCheckbox.disabled = false;
        //invoiceCommentText.disabled = false;
    }
    else {
        notifyCustomerCheckbox.disabled = true;
        //invoiceCommentText.disabled = true;
    }
}

window.bindSendEmail = bindSendEmail;
window.checkButtonsRelation = checkButtonsRelation;
//]]>

});
</script>
